home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / io / randomac.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  13.8 KB  |  528 lines

  1. /*
  2.  * @(#)RandomAccessFile.java    1.23 95/08/18 David Brown
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.io;
  21.  
  22. import java.io.File;
  23.  
  24. /**
  25.  * Random access files can be constructed from file descriptors, file 
  26.  * names, or file objects.  This class provides a sense of security
  27.  * by offering methods that allow specified mode accesses of 
  28.  * read-only or read-write to files.
  29.  */
  30. public
  31. class RandomAccessFile implements DataOutput, DataInput {
  32.     int fd;
  33.  
  34.     /**
  35.      * Creates a RandomAccessFile with the specified system dependent 
  36.      * file name and the specified mode.
  37.      * Mode "r" is for read-only and mode "rw" is for read+write.
  38.      * @param name the system dependent file name
  39.      * @param mode the access mode
  40.      * @exception IOException If an I/O error has occurred.
  41.      */
  42.     public RandomAccessFile(String name, String mode) throws IOException {
  43.     boolean rw = mode.equals("rw");
  44.     if (!rw && !mode.equals("r"))
  45.         throw new IllegalArgumentException("mode must be r or rw");
  46.     SecurityManager security = System.getSecurityManager();
  47.     if (security != null) {
  48.         security.checkRead(name);
  49.         if (rw) {
  50.         security.checkWrite(name);
  51.         }
  52.     }
  53.     open(name, rw);
  54.     }
  55.     
  56.     /**
  57.      * Creates a RandomAccessFile with the specified system dependent
  58.      * file descriptor.
  59.      * @param fd the system dependent file descriptor
  60.      * @exception IOException If an I/O error has occurred.
  61.      */
  62.     public RandomAccessFile(int fd) throws IOException {
  63.     SecurityManager security = System.getSecurityManager();
  64.     if (security != null) {
  65.         security.checkRead(fd);
  66.         security.checkWrite(fd);
  67.     }
  68.     openfd(fd);
  69.     }
  70.     
  71.     /**
  72.      * Creates a RandomAccessFile from a specified File object
  73.      * and mode ("r" or "rw").     
  74.      * @param file the file object
  75.      * @param mode the access mode
  76.      */
  77.     public RandomAccessFile(File file, String mode) throws IOException {
  78.     this(file.getPath(), mode);
  79.     }
  80.  
  81.     /**
  82.      * Opens a file and returns the file descriptor.  The file is 
  83.      * opened in read-write mode if writeable is true, else 
  84.      * the file is opened as read-only.
  85.      * @param name the name of the file
  86.      * @param writeable the boolean indicating whether file is 
  87.      * writeable or not.
  88.      */
  89.     private native void open(String name, boolean writeable) throws IOException;
  90.  
  91.     /**
  92.      * Opens a file with the specified file descriptor.
  93.      * @param fd the file descriptor
  94.      */
  95.     private native void openfd(int fd) throws IOException;
  96.  
  97.     // 'Read' primitives
  98.     
  99.     /**
  100.      * Reads a byte of data. This method will block if no input is
  101.      * available.
  102.      * @return the byte read, or -1 if the end of the
  103.      *          stream is reached. 
  104.      * @exception IOException If an I/O error has occurred.
  105.      */
  106.     public native int read() throws IOException;
  107.  
  108.     /**
  109.      * Reads a sub array as a sequence of bytes. 
  110.      * @param b the data to be written
  111.      * @param off the start offset in the data
  112.      * @param len the number of bytes that are written
  113.      * @exception IOException If an I/O error has occurred.
  114.      */
  115.     private native int readBytes(byte b[], int off, int len) throws IOException;
  116.  
  117.     /**
  118.      * Reads a sub array as a sequence of bytes. 
  119.      * @param b the data to be written
  120.      * @param off the start offset in the data
  121.      * @param len the number of bytes that are written
  122.      * @exception IOException If an I/O error has occurred.
  123.      */
  124.     public int read(byte b[], int off, int len) throws IOException {
  125.     return readBytes(b, off, len);
  126.     }
  127.  
  128.     /**
  129.      * Reads data into an array of bytes.  This method blocks
  130.      * until some input is available.
  131.      * @return the actual number of bytes read, -1 is
  132.      *          returned when the end of the stream is reached.
  133.      * @exception IOException If an I/O error has occurred.    
  134.      */
  135.     public int read(byte b[]) throws IOException {
  136.     return readBytes(b, 0, b.length);
  137.     }
  138.    
  139.     /**
  140.      * Reads bytes, blocking until all bytes are read.
  141.      * @param b    the buffer into which the data is read
  142.      * @return  the actual number of bytes read, -1 is
  143.      *         returned when the end of the stream is reached.
  144.      * @exception IOException If an I/O error has occurred.
  145.      */
  146.     public final void readFully(byte b[]) throws IOException {
  147.     readFully(b, 0, b.length);
  148.     }
  149.  
  150.     /**
  151.      * Reads bytes, blocking until all bytes are read.
  152.      * @param b    the buffer into which the data is read
  153.      * @param off the start offset of the data
  154.      * @param len the maximum number of bytes read
  155.      * @return  the actual number of bytes read, -1 is
  156.      *         returned when the end of the stream is reached.
  157.      * @exception IOException If an I/O error has occurred.
  158.      */
  159.     public final void readFully(byte b[], int off, int len) throws IOException {
  160.     int n = 0;
  161.     while (n < len) {
  162.         int count = this.read(b, off + n, len - n);
  163.         if (count < 0)
  164.         throw new EOFException();
  165.         n += count;
  166.     }
  167.     }
  168.  
  169.  
  170.     public int skipBytes(int n) throws IOException {
  171.         seek(getFilePointer() + n);
  172.         return n;
  173.     }
  174.  
  175.     // 'Write' primitives
  176.  
  177.     /**
  178.      * Writes a byte of data. This method will block until the byte
  179.      * is actually written. 
  180.      * @param b the byte to be written
  181.      * @exception IOException If an I/O error has occurred. 
  182.      */
  183.     public native void write(int b) throws IOException;
  184.  
  185.     /**
  186.      * Writes a sub array as a sequence of bytes. 
  187.      * @param b the data to be written
  188.      * @param off the start offset in the data
  189.      * @param len the number of bytes that are written
  190.      * @exception IOException If an I/O error has occurred.
  191.      */
  192.     private native void writeBytes(byte b[], int off, int len) throws IOException;
  193.  
  194.     /**
  195.      * Writes an array of bytes. Will block until the bytes
  196.      * are actually written. 
  197.      * @param b the data to be written
  198.      * @exception IOException If an I/O error has occurred.
  199.      */
  200.     public void write(byte b[]) throws IOException {
  201.     writeBytes(b, 0, b.length); 
  202.     }
  203.  
  204.     /**
  205.      * Wrotes a sub array of bytes.
  206.      * @param b the data to be written
  207.      * @param off the start offset in the data
  208.      * @param len the number of bytes that are written
  209.      * @exception IOException If an I/O error has occurred. 
  210.      */
  211.     public void write(byte b[], int off, int len) throws IOException {
  212.     writeBytes(b, off, len);
  213.     }
  214.  
  215.     // 'Random access' stuff
  216.  
  217.  
  218.     /**
  219.      * Returns the current location of the file pointer.
  220.      */
  221.     public native long getFilePointer() throws IOException;
  222.  
  223.     /**
  224.      * Sets the file pointer to the specified absolute position.
  225.      * @param pos the absolute position
  226.      */
  227.     public native void seek(long pos) throws IOException;
  228.  
  229.     /**
  230.      * Returns the length of the file.
  231.      */
  232.     public native long length() throws IOException;
  233.  
  234.     /**
  235.      * Closes the file. 
  236.      * @exception IOException If an I/O error has occurred.
  237.      */
  238.     public native void close() throws IOException;
  239.  
  240.  
  241.     //
  242.     //  Some "reading/writing Java data types" methods stolen from
  243.     //  DataInputStream and DataOutputStream.
  244.     //
  245.  
  246.     /**
  247.      * Reads a boolean.
  248.      */
  249.     public final boolean readBoolean() throws IOException {
  250.     int ch = this.read();
  251.     if (ch < 0)
  252.         throw new EOFException();
  253.     return (ch != 0);
  254.     }
  255.  
  256.     /**
  257.      * Reads a byte.
  258.      */
  259.     public final byte readByte() throws IOException {
  260.     int ch = this.read();
  261.     if (ch < 0)
  262.         throw new EOFException();
  263.     return (byte)(ch);
  264.     }
  265.  
  266.  
  267.     /**
  268.      * Reads an unsigned 8 bit byte.
  269.      * @return the 8 bit byte read.
  270.      */
  271.     public final int readUnsignedByte() throws IOException {
  272.     int ch = this.read();
  273.     if (ch < 0)
  274.         throw new EOFException();
  275.     return ch;
  276.     }
  277.  
  278.  
  279.     /**
  280.      * Reads 16 bit short.
  281.      * @return the read 16 bit short.
  282.      */
  283.     public final short readShort() throws IOException {
  284.     int ch1 = this.read();
  285.     int ch2 = this.read();
  286.     if ((ch1 | ch2) < 0)
  287.          throw new EOFException();
  288.     return (short)((ch1 << 8) + (ch2 << 0));
  289.     }
  290.  
  291.  
  292.     /**
  293.      * Reads 16 bit short.
  294.      * @return the read 16 bit short.
  295.      */
  296.     public final int readUnsignedShort() throws IOException {
  297.     int ch1 = this.read();
  298.     int ch2 = this.read();
  299.     if ((ch1 | ch2) < 0)
  300.          throw new EOFException();
  301.     return (ch1 << 8) + (ch2 << 0);
  302.     }
  303.  
  304.  
  305.     /**
  306.      * Reads a 16 bit char.
  307.      * @return the read 16 bit char. 
  308.      */
  309.     public final char readChar() throws IOException {
  310.     int ch1 = this.read();
  311.     int ch2 = this.read();
  312.     if ((ch1 | ch2) < 0)
  313.          throw new EOFException();
  314.     return (char)((ch1 << 8) + (ch2 << 0));
  315.     }
  316.  
  317.     /**
  318.      * Reads a 32 bit int.
  319.      * @return the read 32 bit integer.
  320.      */
  321.     public final int readInt() throws IOException {
  322.     int ch1 = this.read();
  323.     int ch2 = this.read();
  324.     int ch3 = this.read();
  325.     int ch4 = this.read();
  326.     if ((ch1 | ch2 | ch3 | ch4) < 0)
  327.          throw new EOFException();
  328.     return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
  329.     }
  330.  
  331.     /**
  332.      * Reads a 64 bit long.
  333.      * @return the read 64 bit long.
  334.      */
  335.     public final long readLong() throws IOException {
  336.     return (this.readInt() << 32L) + (this.readInt() & 0xFFFFFFFFL);
  337.     }
  338.  
  339.     /**
  340.      * Reads a 32 bit float.
  341.      * @return the read 32 bit float.
  342.      */
  343.     public final float readFloat() throws IOException {
  344.     return Float.intBitsToFloat(readInt());
  345.     }
  346.  
  347.     /**
  348.      * Reads a 64 bit double.
  349.      * @return the read 64 bit double.
  350.      */
  351.     public final double readDouble() throws IOException {
  352.     return Double.longBitsToDouble(readLong());
  353.     }
  354.  
  355.     /**
  356.      * Reads a line terminated by a '\n' or EOF.
  357.      */
  358.     public final String readLine() throws IOException {
  359.     StringBuffer input = new StringBuffer();
  360.     int c;
  361.  
  362.     while (((c = read()) != -1) && (c != '\n')) {
  363.         input.append((char)c);
  364.     }
  365.     if ((c == -1) && (input.length() == 0)) {
  366.         return null;
  367.     }
  368.     return input.toString();
  369.     }
  370.  
  371.     /**
  372.      * Reads a UTF formatted String.
  373.      */
  374.     public final String readUTF() throws IOException {
  375.     return DataInputStream.readUTF(this);
  376.     }
  377.  
  378.     /**
  379.      * Writes a boolean.
  380.      * @param v the boolean value
  381.      */
  382.     public final void writeBoolean(boolean v) throws IOException {
  383.     write(v ? 1 : 0);
  384.     //written++;
  385.     }
  386.  
  387.     /**
  388.      * Writes a byte.
  389.      * @param v the byte
  390.      */
  391.     public final void writeByte(int v) throws IOException {
  392.     write(v);
  393.     //written++;
  394.     }
  395.  
  396.     /**
  397.      * Writes a short.
  398.      * @param v the short
  399.      */
  400.     public final void writeShort(int v) throws IOException {
  401.     write((v >>> 8) & 0xFF);
  402.     write((v >>> 0) & 0xFF);
  403.     //written += 2;
  404.     }
  405.  
  406.     /**
  407.      * Writes a character.
  408.      * @param v the char
  409.      */
  410.     public final void writeChar(int v) throws IOException {
  411.     write((v >>> 8) & 0xFF);
  412.     write((v >>> 0) & 0xFF);
  413.     //written += 2;
  414.     }
  415.  
  416.     /**
  417.      * Writes an integer.
  418.      * @param v the integer
  419.      */
  420.     public final void writeInt(int v) throws IOException {
  421.     write((v >>> 24) & 0xFF);
  422.     write((v >>> 16) & 0xFF);
  423.     write((v >>>  8) & 0xFF);
  424.     write((v >>>  0) & 0xFF);
  425.     //written += 4;
  426.     }
  427.  
  428.     /**
  429.      * Writes a long.
  430.      * @param v the long
  431.      */
  432.     public final void writeLong(long v) throws IOException {
  433.     write((int)(v >>> 56) & 0xFF);
  434.     write((int)(v >>> 48) & 0xFF);
  435.     write((int)(v >>> 40) & 0xFF);
  436.     write((int)(v >>> 32) & 0xFF);
  437.     write((int)(v >>> 24) & 0xFF);
  438.     write((int)(v >>> 16) & 0xFF);
  439.     write((int)(v >>>  8) & 0xFF);
  440.     write((int)(v >>>  0) & 0xFF);
  441.     //written += 8;
  442.     }
  443.  
  444.     /*
  445.      * Writes a 32 bit float.
  446.      * @param v the float value to be written
  447.      */
  448.     public final void writeFloat(float v) throws IOException {
  449.     writeInt(Float.floatToIntBits(v));
  450.     }
  451.  
  452.  
  453.     /*
  454.      * Writes a 64 bit double.
  455.      * @param v the double value to be written
  456.      */
  457.     public final void writeDouble(double v) throws IOException {
  458.     writeLong(Double.doubleToLongBits(v));
  459.     }
  460.  
  461.  
  462.     /**
  463.      * Writes a String as a sequence of bytes.
  464.      * @param s the String
  465.      */
  466.     public final void writeBytes(String s) throws IOException {
  467.     int len = s.length();
  468.     for (int i = 0 ; i < len ; i++) {
  469.         write((byte)s.charAt(i));
  470.     }
  471.     //written += len;
  472.     }
  473.  
  474.     /**
  475.      * Writes a String as a sequence of chars.
  476.      * @param s the String
  477.      */
  478.     public final void writeChars(String s) throws IOException {
  479.     int len = s.length();
  480.     for (int i = 0 ; i < len ; i++) {
  481.         int v = s.charAt(i);
  482.         write((v >>> 8) & 0xFF);
  483.         write((v >>> 0) & 0xFF);
  484.     }
  485.     //written += len * 2;
  486.     }
  487.  
  488.     /**
  489.      * Writes a String in UTF format.
  490.      * @param str the String
  491.      */
  492.     public final void writeUTF(String str) throws IOException {
  493.     int strlen = str.length();
  494.     int utflen = 0;
  495.  
  496.     for (int i = 0 ; i < strlen ; i++) {
  497.         int c = str.charAt(i);
  498.         if ((c >= 0x0001) && (c <= 0x007F)) {
  499.         utflen++;
  500.         } else if (c > 0x07FF) {
  501.         utflen += 3;
  502.         } else {
  503.         utflen += 2;
  504.         }
  505.     }
  506.  
  507.     write((utflen >>> 8) & 0xFF);
  508.     write((utflen >>> 0) & 0xFF);
  509.     for (int i = 0 ; i < strlen ; i++) {
  510.         int c = str.charAt(i);
  511.         if ((c >= 0x0001) && (c <= 0x007F)) {
  512.         write(c);
  513.         } else if (c > 0x07FF) {
  514.         write(0xE0 | ((c >> 12) & 0x0F));
  515.         write(0x80 | ((c >>  6) & 0x3F));
  516.         write(0x80 | ((c >>  0) & 0x3F));
  517.         //written += 2;
  518.         } else {
  519.         write(0xC0 | ((c >>  6) & 0x1F));
  520.         write(0x80 | ((c >>  0) & 0x3F));
  521.         //written += 1;
  522.         }
  523.     }
  524.     //written += strlen + 2;
  525.     }
  526.  
  527. }
  528.